package com.irdstudio.apicenter.console.controller.template;

import java.io.IOException;

import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.util.FootPage;
import com.irdstudio.apicenter.console.dm.util.FootPageDao;
import com.irdstudio.apicenter.console.service.template.SPageParamMainService;
import com.irdstudio.apicenter.console.service.template.SPageParamTagService;
import com.irdstudio.apicenter.frame.core.jdbc.tablemodel.ITableModelDAO;
import com.irdstudio.apicenter.frame.maker.dm.SPageParamMain;
import com.irdstudio.apicenter.frame.maker.dm.SPageParamTag;
import com.irdstudio.apicenter.frame.util.BeanUtils;

/**
 * app应用管理
 * 
 * @author jr
 * 
 */
@Controller
@RequestMapping(value = "/sPageParamTag")
public class SPageParamTagAction extends MacmsBaseAction {
	@Autowired
	private SPageParamTagService sPageParamTagService;
	@Autowired
	private SPageParamMainService sPageParamMainService;

	@RequestMapping(value = "querySPageParamTagList")
	public String queryByPageId(HttpServletRequest request) throws Exception {
		// String PAGE_ID=request.getParameter("pageId");
		String PAGE_ID = request.getParameter("pageId");
		String tagName = "标签";
		tagName = new String(request.getParameter("tagName").getBytes("iso-8859-1"),"utf-8");
		if (StringUtils.isEmpty(PAGE_ID)) {
			throw new Exception("必须传入页面标识(PAGE_ID)参数!");
		}

		String qbcode = "";
		String qbname = "";
		if (request.getParameter("qbcode") != null) {
			qbcode = request.getParameter("qbcode");
		}
		if (request.getParameter("qbname") != null) {
			qbname = request.getParameter("qbname");
		}

		int row = 0;
		int size = 0;
		row = (null == request.getParameter("pageNo") ? 0 : Integer
				.parseInt(request.getParameter("pageNo")));
		size = (null == request.getParameter("pageSize") ? 0 : Integer
				.parseInt(request.getParameter("pageSize")));

		FootPageDao fpd = new FootPageDao();
		FootPage fp = new FootPage();

		fp = fpd.querypage("  s_page_param_tag a  where    a.PAGE_ID= '"
				+ PAGE_ID + "'  and  a.TAG_NAME  like  '%" + qbname
				+ "%'  and  a.TEMPLATE_PARAM_CODE  like  '%" + qbcode + "%'",
				row, size);
		row = (fp.getPageNo() - 1) * fp.getPageSize();

		String sql = "SELECT * FROM s_page_param_tag  a WHERE PAGE_ID='"
				+ PAGE_ID + "'  and  a.TAG_NAME  like  '%" + qbname
				+ "%'  and  a.TEMPLATE_PARAM_CODE  like  '%" + qbcode + "%'";

		ITableModelDAO tableModelDao = (ITableModelDAO) BeanUtils
				.getBean("tableModelDAO");
		sql = tableModelDao.getNativePageSql(sql, row, fp.getPageSize());
		List<SPageParamTag> pageParamTagList = sPageParamTagService
				.queryByPageId(sql);
		List<SPageParamMain> pageParamList = sPageParamMainService
				.queryByPageId(PAGE_ID);
		request.setAttribute("pageParamList", pageParamList);
		request.setAttribute("pageParamTagList", pageParamTagList);
		request.setAttribute("pageId", PAGE_ID);
		request.setAttribute("page", fp);
		request.setAttribute("templateParamName", tagName);
		request.setAttribute("tagisshow", "1");
		//return "admin/spageParamTagList";
		return "admin/pageTemplateParam";
	}

	@RequestMapping(value = "saveSPageParamTag")
	public void saveSPageParamTag(
			@ModelAttribute("sPageParamTag") SPageParamTag sPageParamTag,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		SPageParamTag sPageParamTag1 = sPageParamTagService
				.queryWithKeys(sPageParamTag.getRecordKey());
		if (sPageParamTag1 != null) {
			flag = sPageParamTagService.update(sPageParamTag);
		} else {
			flag = sPageParamTagService.insert(sPageParamTag);
		}

		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}

	}

	// 跳到新增和修改的界面
	@RequestMapping(value = "jumpCreatePage")
	public String toDialog(HttpServletRequest request,
			HttpServletResponse response) throws SQLException {
		String recordKey = request.getParameter("recordKey");
		String pageId = request.getParameter("pageId");
		String paramCode = request.getParameter("paramCode");
		SPageParamTag sPageParamTag = sPageParamTagService
				.queryWithKeys(recordKey);
		request.setAttribute("sPageParamTag", sPageParamTag);
		request.setAttribute("recordKey", recordKey);
		request.setAttribute("paramcode", paramCode);
		request.setAttribute("pageId", pageId);
		return "admin/sPageParamTag";
	}

	// 跳到查看的界面
	@RequestMapping(value = "jumpViewPage")
	public String jumpViewPage(HttpServletRequest request,
			HttpServletResponse response) throws SQLException {
		String recordKey = request.getParameter("recordKey");
		String pageId = request.getParameter("pageId");
		SPageParamTag sPageParamTag = sPageParamTagService
				.queryWithKeys(recordKey);
		request.setAttribute("sPageParamTag", sPageParamTag);
		request.setAttribute("recordKey", recordKey);
		request.setAttribute("pageId", pageId);
		return "admin/viewsPageParamTag";
	}

	@RequestMapping(value = "deleteSPageParamTag")
	public void deleteSPageParamTag(HttpServletRequest request,
			HttpServletResponse response) throws SQLException,
			ClassNotFoundException {
		int flag = -1;
		String record_key = request.getParameter("recordKey");
		flag = sPageParamTagService.delete(record_key);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}

	}

}
